#!bin/bash

PATTERN_PATH=/mnt/e/project-pattern/

# 安装nginx
if [[ -z "$(docker images -q nginx:latest 2> /dev/null)" ]]; then
     docker pull nginx:latest
else
    echo "nginx installed"
fi

#	运行 -p 80:80： 端口进行映射，将本地 9526 端口映射到容器内部的 80 端口。
  if [[ -z "$(docker ps -aqf "name=nginx-pattern")" ]]; then
    docker run --name nginx-pattern -p 80:80 -p 443:443 \
    --restart=always \
    --privileged=true \
    -v /opt/web/dist:/usr/share/nginx/html/dist:rw \
    --add-host=project-pattern:192.168.0.101 \
    -d nginx
    #上传配置文件
       docker cp $PATTERN_PATH/.environment/init/docker/nginx/nginx.conf nginx-pattern:/opt/
       docker cp $PATTERN_PATH/.environment/init/docker/nginx/conf.d/127.0.0.1.conf nginx-pattern:/opt/
       docker cp $PATTERN_PATH/.environment/init/docker/nginx/server.key nginx-pattern:/etc/nginx/
       docker cp $PATTERN_PATH/.environment/init/docker/nginx/server.pem nginx-pattern:/etc/nginx/
    sleep 3s
    # 替换容器nginx 脚本
       docker exec -i nginx-pattern bash -c "cp /opt/nginx.conf /etc/nginx/;rm -rf /etc/nginx/conf.d/*;cp /opt/127.0.0.1.conf /etc/nginx/conf.d/"
    #重新reload nginx
       docker restart nginx-pattern
  else
      #替换nginx配置
      docker cp $PATTERN_PATH/.environment/init/docker/nginx/nginx.conf nginx-pattern:/opt/
      docker cp $PATTERN_PATH/.environment/init/docker/nginx/conf.d/127.0.0.1.conf nginx-pattern:/opt/
      docker cp $PATTERN_PATH/.environment/init/docker/nginx/server.key nginx-pattern:/etc/nginx/
      docker cp $PATTERN_PATH/.environment/init/docker/nginx/server.pem nginx-pattern:/etc/nginx/
      docker exec -i nginx-pattern bash -c "cp /opt/nginx.conf /etc/nginx/;rm -rf /etc/nginx/conf.d/*;cp /opt/127.0.0.1.conf /etc/nginx/conf.d/"
      docker restart nginx-pattern
      echo "nginx-pattern running"
  fi




